package polymorphic;

/**
 * @Package: polymorphic
 * @ClassName: Dynamic
 * @Author: x2606
 * @CreateTime: 2022/2/23 22:39
 * @Description: 动态绑定
 */

public class DynamicBinding {
    public static void main(String[] args) {

        //1. 当调用对象方法的时候，该方法会和该对象的内存地址/运行类型绑定。
        //2. 当调用对象属性时，没有动态绑定机制， 就近原则调用属性。

         A a = new B();
        System.out.println(a.getI());
        System.out.println(a.sum());
        System.out.println(a.sum1());
        //输出结果
        //100
        //200
        //300
    }
}

class A {
    public int i=10;
    public int getI(){
        return i;
    }
    public int sum(){
        return getI()+10;
    }
    public int sum1(){
        return i+10;
    }
}

class B extends A{
    public int i=100;
    public int getI(){
        return i;
    }
    public int sum(){ return i+100; }
//    public int sum1(){
//    return i+100;
//}
}
